package com.zaimokuza.cn.controller;

import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.Tag;
import com.zaimokuza.cn.entity.response.ResponseFormat;
import com.zaimokuza.cn.service.TagService;
import com.zaimokuza.cn.util.RandomUtils;
import com.zaimokuza.cn.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Api(tags = "标签相关")
@ResponseFormat
@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    private TagService tagService;

    @ApiOperation(value = "添加标签", notes = "添加标签")
    @PostMapping("/add")
    public void addTag(@Validated @RequestBody Tag tag) {
        // 组装标签信息
        tag.setId(RandomUtils.get32Uid());
        tag.setUserId(UserUtils.getUserId());
        // 添加标签
        tagService.addTag(tag);
    }

    @ApiOperation(value = "更新标签", notes = "更新标签")
    @PutMapping("/update")
    public void updateTag(@Validated @RequestBody Tag tag) {
        tagService.updateTag(tag);
    }

    @ApiOperation(value = "删除标签", notes = "根据ID删除标签")
    @DeleteMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public void deleteTag(@RequestParam @ApiParam("标签ID") String id) {
        tagService.deleteTag(id);
    }

    @ApiOperation(value = "查询标签列表", notes = "根据用户ID查询标签列表")
    @GetMapping("/list")
    public ArrayList<Tag> getTagList() {
        return tagService.getTagList(UserUtils.getUserId());
    }

    @ApiOperation(value = "查询分页的分类列表", notes = "查询分页的分类列表")
    @GetMapping("/page")
    public PageResult<Tag> getClassificationPage(@RequestParam(required = false) @ApiParam("标签名称") String name,
                                                 @RequestParam(required = false) @ApiParam("用户名称") String userName,
                                                 @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                 @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return tagService.getTagPage(name, userName, page, size);
    }

}
